
package com.insigma.business.qrcode.soldier.service.impl;


import com.insigma.business.qrcode.common.dao.CommonAdminAreaDO;
import com.insigma.business.qrcode.common.dto.BaseLogRequest;
import com.insigma.business.qrcode.common.dto.ContactNoBO;
import com.insigma.business.qrcode.common.service.CommonAdminAreaService;
import com.insigma.business.qrcode.common.util.AdminAreaCodeUtils;
import com.insigma.business.qrcode.common.util.RedisUtils;
import com.insigma.business.qrcode.soldier.dao.SystemConfigDao;
import com.insigma.business.qrcode.soldier.dto.SystemConfigService;
import com.insigma.business.qrcode.soldier.entity.BaseLogDO;
import com.insigma.business.qrcode.soldier.entity.SystemConfigDO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.insigma.business.qrcode.common.util.CommonConstant;

import javax.annotation.Resource;
import java.util.List;


@Service("systemConfigService")
public class SystemConfigServiceImpl implements SystemConfigService {

    @Autowired(required = false)
    private SystemConfigDao systemConfigDao;

    @Resource
    RedisUtils redisUtils;

    @Autowired
    private CommonAdminAreaService commonAdminAreaService;
    @Override
    public List<SystemConfigDO> list() {
        return systemConfigDao.list();

    }

    @Override
    public SystemConfigDO getByName(final String name) {
        return systemConfigDao.getByName(name);
    }

    @Override
    public List<SystemConfigDO> listByNameLike(String nameLike) {
        return systemConfigDao.listByNameLike(nameLike);

    }

    @Override
    public int update(SystemConfigDO systemConfig) {
        return systemConfigDao.update(systemConfig);

    }

    @Override
    public SystemConfigDO get(Long id) {
        return systemConfigDao.get(id);

    }

    @Override
    public ContactNoBO getCachedContactNo(String adminCode) {
        String prefixDistrictContactNo = CommonConstant.PREFIX_DISTRICT_CONTACT_NO;
        String fillAreaCode = AdminAreaCodeUtils.fillAreaCode(adminCode.substring(0, 6));
        String key = prefixDistrictContactNo + fillAreaCode;

        String prefix = "REDIS_KEY";
        String redisKey = prefix + key;
        Object o = redisUtils.get(redisKey);
        if (o != null) {
            return (ContactNoBO) o;
        }
        SystemConfigDO configDo = getByName(key);
        CommonAdminAreaDO commonAdminAreaDO = commonAdminAreaService.get(adminCode);
        if (commonAdminAreaDO == null) {
            return null;
        }

        ContactNoBO dto = new ContactNoBO();
        dto.setAdminAreaCode(adminCode);
        dto.setAdminAreaValue(commonAdminAreaDO.getValue());
        if (configDo != null) {
            dto.setContactNo(configDo.getValue());
            dto.setRemark(configDo.getRemark());
        }

        redisUtils.set(redisKey, dto, 60 * 60);
        return dto;    }

    @Override
    public void insertLog(BaseLogRequest insertRequest) {
        BaseLogDO baseLogDO = new BaseLogDO();
        BeanUtils.copyProperties(insertRequest, baseLogDO);
        systemConfigDao.insertLog(baseLogDO);
    }


}
